package com.me.myFramework.myRPC1;

import com.me.study.designPatterns.aProxy.SellTicket;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Random;

/**
 * RPC 消费者
 *
 * @author ME
 * @date   2020/2/24
 */
public class RpcConsumer {

    public static void main(String[] args) throws Exception {
        Random random = new Random();
        for (int i = 1; i <= 10; i ++) {
            System.out.printf("\n======================= 调用%d ========================", i);

            int tempInt = random.nextInt(3);
            if (tempInt == 0) {
                // 无注册中心，直接引用服务端创建代理
                HelloService service = RpcFramework.reference(HelloService.class, "127.0.0.1", 1234);
                System.out.println(service.hello(RandomStringUtils.random(3, true, true)));
            } else if (tempInt == 1) {
                // 无注册中心，直接引用服务端创建代理
                SellTicket service = RpcFramework.reference(SellTicket.class, "127.0.0.1", 1234);
                service.sellTicket();
                System.out.println("service.sellTicket()");
            } else {
                try {
                    // TODO 模拟远程方法异常
                    HelloService service = RpcFramework.reference(HelloService.class, "127.0.0.1", 1234);
                    service.hello2("World");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            Thread.sleep(1000);
        }
    }

}
